package com.sunwayhorizo.health.hl7.persistent;

import com.sunwayhorizo.health.hl7.datatypes.common.ADXPImpl;
import com.sunwayhorizo.health.hl7.datatypes.common.CSImpl;
import com.sunwayhorizo.health.hl7.persistent.pk.RimCoreRoleAddrsPK;

import java.io.Serializable;

import java.sql.Timestamp;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.TableGenerator;


@Entity
//@TableGenerator(name="RoleAddrs_IdGenerator", table="RIM_Id_Generator", pkColumnName = "pkColumn", valueColumnName = "valueColumn",pkColumnValue="Role_Addrs")
//@NamedQuery(name = "RimCoreRoleAddrs.findAll",     query = "select o from RimCoreRoleAddrs o")
@Table(name = "RIM_CORE_ROLE_ADDRS")
@IdClass(RimCoreRoleAddrsPK.class)
public class RimCoreRoleAddrs implements Serializable {
    @Id
    @Column(name="ROLE_ID", nullable = false, insertable = false,  updatable = false)    private Long roleId;
    @Id
    @Column(name="ROLE_VERSION_NUM", nullable = false, insertable = false,  updatable = false)    private Long roleVersionNum;
    @Id
//    @GeneratedValue(strategy = GenerationType.TABLE,generator = "RoleAddrs_IdGenerator" )
    @Column(name="ADDRESS_ID", nullable = false)    private Long addressId;
    
   
    @Column(name="EFFECTIVE_START_DATE_TS")    private Timestamp effectiveStartDateTs;
    @Column(name="EFFECTIVE_END_DATE_TS")    private Timestamp effectiveEndDateTs;
    @Column(name="EFFECTIVE_GTS_TXT")    private String literal;
    
    @OneToMany(mappedBy = "rimCoreRoleAddrs")    private List<RimCoreRoleAddrUses> rimCoreRoleAddrUsesList = new ArrayList();
    @OneToMany(mappedBy = "rimCoreRoleAddrs")    private List<RimCoreRoleAddrParts> rimCoreRoleAddrPartsList = new ArrayList();
    
    @ManyToOne
    @JoinColumns({
    @JoinColumn(name = "ROLE_ID", referencedColumnName = "ROLE_ID"),
    @JoinColumn(name = "ROLE_VERSION_NUM", referencedColumnName = "ROLE_VERSION_NUM")
    })
    private RimCoreRoles rimCoreRoles;

    public RimCoreRoleAddrs() {
    }

    private Long getAddressId() {
        return addressId;
    }

    private void setAddressId(Long addressId) {
        this.addressId = addressId;
    }

    private Timestamp getEffectiveEndDateTs() {
        return effectiveEndDateTs;
    }

    private void setEffectiveEndDateTs(Timestamp effectiveEndDateTs) {
        this.effectiveEndDateTs = effectiveEndDateTs;
    }

    private Timestamp getEffectiveStartDateTs() {
        return effectiveStartDateTs;
    }

    private void setEffectiveStartDateTs(Timestamp effectiveStartDateTs) {
        this.effectiveStartDateTs = effectiveStartDateTs;
    }

    private Long getRoleId() {
        return roleId;
    }

    private void setRoleId(Long roleId) {
        this.roleId = roleId;
    }

    private Long getRoleVersionNum() {
        return roleVersionNum;
    }

    private void setRoleVersionNum(Long roleVersionNum) {
        this.roleVersionNum = roleVersionNum;
    }

    public List<RimCoreRoleAddrUses> getRimCoreRoleAddrUsesList() {
        return rimCoreRoleAddrUsesList;
    }

    public void setRimCoreRoleAddrUsesList(List<RimCoreRoleAddrUses> rimCoreRoleAddrUsesList) {
        this.rimCoreRoleAddrUsesList = rimCoreRoleAddrUsesList;
    }

    public RimCoreRoleAddrUses addRimCoreRoleAddrUses(RimCoreRoleAddrUses rimCoreRoleAddrUses) {
        getRimCoreRoleAddrUsesList().add(rimCoreRoleAddrUses);
        rimCoreRoleAddrUses.setRimCoreRoleAddrs(this);
        return rimCoreRoleAddrUses;
    }

    public RimCoreRoleAddrUses removeRimCoreRoleAddrUses(RimCoreRoleAddrUses rimCoreRoleAddrUses) {
        getRimCoreRoleAddrUsesList().remove(rimCoreRoleAddrUses);
        rimCoreRoleAddrUses.setRimCoreRoleAddrs(null);
        return rimCoreRoleAddrUses;
    }
    
    public void setRoleAddrUses(Set mSet){
        CSImpl use = (CSImpl)mSet.iterator().next();
        RimCoreRoleAddrUses roleAddrUses = new RimCoreRoleAddrUses();
        roleAddrUses.setUse(use.code().toString());
        this.addRimCoreRoleAddrUses(roleAddrUses);            
    }

    public List<RimCoreRoleAddrParts> getRimCoreRoleAddrPartsList() {
        return rimCoreRoleAddrPartsList;
    }

    public void setRimCoreRoleAddrPartsList(List<RimCoreRoleAddrParts> rimCoreRoleAddrPartsList) {
        this.rimCoreRoleAddrPartsList = rimCoreRoleAddrPartsList;
    }

    public RimCoreRoleAddrParts addRimCoreRoleAddrParts(RimCoreRoleAddrParts rimCoreRoleAddrParts) {
        getRimCoreRoleAddrPartsList().add(rimCoreRoleAddrParts);
        rimCoreRoleAddrParts.setRimCoreRoleAddrs(this);
        return rimCoreRoleAddrParts;
    }

    public RimCoreRoleAddrParts removeRimCoreRoleAddrParts(RimCoreRoleAddrParts rimCoreRoleAddrParts) {
        getRimCoreRoleAddrPartsList().remove(rimCoreRoleAddrParts);
        rimCoreRoleAddrParts.setRimCoreRoleAddrs(null);
        return rimCoreRoleAddrParts;
    }

    public void setRoleAddrParts(List mList){
        for (int i = 0 ;i < mList.size() ; i++ )  {
            ADXPImpl adxp = (ADXPImpl)mList.get(i);
            RimCoreRoleAddrParts roleAddrParts = new RimCoreRoleAddrParts();
            roleAddrParts.setPartOrderNum(new Long(i));
            roleAddrParts.setPartType(adxp.partType().toString());
            roleAddrParts.setLiteral(adxp.literal().toString());
            this.addRimCoreRoleAddrParts(roleAddrParts);        
        }    
    }
    public void setRimCoreRoles(RimCoreRoles rimCoreRoles) {
        this.rimCoreRoles = rimCoreRoles;
    }

    public RimCoreRoles getRimCoreRoles() {
        return rimCoreRoles;
    }

    public void setLiteral(String literal) {
        this.literal = literal;
    }

    public String getLiteral() {
        return literal;
    }
}
